<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>regist</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
</head>
<body>
<div style="text-align: center">
    <br>
    邮箱:<input type="text" name="email" id="email" value="dchuan680@163.com">
    <br>
    <br>
    密码:<input type="text" name="password" id="password" value="111111">
    <br><br>
    确认密码:<input type="text" name="confimPassword" value="111111">
    <br><br>
    验证码:<input type="text" name="code" id="code" value="">
    <button id="getCode">获取验证码</button>
    <br><br>
    <input type="submit" id="toRegist" value="注册">
    <br><br>
</div>

<script>
    function getCode() {
        var email = $('#email').val();
        var password = $('#password').val();
        if (email == "" || password == "") {
            alert('账号密码均不能为空!')
        } else {
            $.ajax({
                url: 'http://127.0.0.1/api/code/create/' + email,
                success: function (result) {
                    timer = setInterval("CountDown()", 1000);
                    document.getElementById("getCode").disabled = true;
                }
            })
        }
    }

    $('#getCode').click(function () {
        getCode();
    })
    $('#toRegist').click(function () {
        var email = $('#email').val();
        var password = $('#password').val();
        var code = $('#code').val();
        if (email == "" || password == "") {
            alert('账号密码均不能为空!')
        } else {
            $.ajax({
                url: 'http://127.0.0.1/api/user/register/'+email+'/'+password+'/'+code,
                success: function (result) {
                    if (result){
                        alert("注册成功")
                        window.location.href="http://127.0.0.1/static/login.html";
                    }
                }
            })
        }
    })
    var timer;
    var maxtime = 10;

    function CountDown() {
        if (maxtime >= 0) {
            $('#getCode').html(maxtime)
            --maxtime;
        } else {
            document.getElementById("getCode").disabled = false;
            $('#getCode').html("获取验证码");
            maxtime = 10;
            clearInterval(timer);
        }
    }
</script>
</body>
</html>